﻿#define USE_STATICVAR
//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Windows;
using System.Threading;
using System.Collections.Generic;
using Microsoft.Practices.Composite.Events;
using Microsoft.WVB.Silverlight.Infrastructure.Events;


namespace Microsoft.WVB.Silverlight.Infrastructure
{
    public class Constants
    {
        public static readonly string DIContainer = "IoC.DIContainer";
        public static readonly string Mediator = "IoC.Mediator";
        public static readonly string SynchronizationContext = "IoC.SynchronizationContext";
    }
#if USE_STATICVAR
    public static class IoC
    {
        public static Dictionary<string, object> StaticResources = new Dictionary<string, object>();
        public static void InitializeApplicationResources(Application app)
        {
            StaticResources.Add(Constants.DIContainer, new DIContainer());
            //StaticResources.Add(Constants.Mediator, new Mediator());
            // Assuming the initialization is done on the main UI Thread
            StaticResources.Add(Constants.SynchronizationContext, SynchronizationContext.Current);
        }
        public static IDIContainer CurrentApplicationDIContainer
        {
            get
            {
                if (!StaticResources.ContainsKey(Constants.DIContainer))
                    throw new NullReferenceException("No DI container was registered");
                return StaticResources[Constants.DIContainer] as IDIContainer;
            }
        }
        //public static IMediator CurrentApplicationMediator
        //{
        //    get
        //    {
        //        if (!StaticResources.ContainsKey(Constants.Mediator))
        //            throw new NullReferenceException("No Mediator was registered");
        //        return StaticResources[Constants.Mediator] as IMediator;
        //    }
        //}
        public static SynchronizationContext SyncContext
        {
            get
            {
                if (!StaticResources.ContainsKey(Constants.SynchronizationContext))
                    throw new NullReferenceException("No SynchronizationContext was registered");
                return StaticResources[Constants.SynchronizationContext] as SynchronizationContext;
            }
        }

        //Helper functions        
        public static T Create<T>()
        {
            return CurrentApplicationDIContainer.Create<T>();
        }
        public static T GetCreate<T>()
        {
            return CurrentApplicationDIContainer.GetCreate<T>();
        }
        public static void CreateStore<T>()
        {
            CurrentApplicationDIContainer.CreateStore<T>();
        }
        public static T Get<T>()
        {
            return CurrentApplicationDIContainer.Get<T>();
        }
        public static T GetNamedConfigurationItem<T>(string name)
        {
            return CurrentApplicationDIContainer.GetNamedConfigurationItem<T>(name);
        }
        public static void Register<T>(DIContainer.Creator creator)
        {
            CurrentApplicationDIContainer.Register<T>(creator);
        }
        public static void RegisterInstance<T>(T t)
        {
            CurrentApplicationDIContainer.RegisterInstance<T>(t);
        }
        public static void RemoveInstance<T>()
        {
            CurrentApplicationDIContainer.RemoveInstance<T>();
        }
        public static void SendNotification<T>(string topic, T notification) 
        {
            IEventAggregator aggr = IoC.GetCreate<IEventAggregator>();
            //TopicDataEventArgs<T> e = new TopicDataEventArgs<T>(topic, notification);
            //aggr.GetEvent<CompositeSilverlightEvent<TopicDataEventArgs<T>>>().Publish(e);
            TopicDataEventArgs<Object> e = new TopicDataEventArgs<Object>(topic, (object)notification);
            aggr.GetEvent<CompositeSilverlightEvent<TopicDataEventArgs<Object>>>().Publish(e);
        }
    }
#else
    public static class IoC
    {
        public static void InitializeApplicationResources(Application app)
        {
            app.Resources.Add(Constants.DIContainer, new DIContainer());
            app.Resources.Add(Constants.Mediator, new Mediator());
            // Assuming the initialization is done on the main UI Thread
            app.Resources.Add(Constants.SynchronizationContext, SynchronizationContext.Current);
        }
        public static IDIContainer CurrentApplicationDIContainer
        {
            get
            {
                if (!Application.Current.Resources.Contains(Constants.DIContainer))
                    throw new NullReferenceException("No DI container was registered");
                return Application.Current.Resources[Constants.DIContainer] as IDIContainer;
            }
        }
        public static IMediator CurrentApplicationMediator
        {
            get
            {
                if (!Application.Current.Resources.Contains(Constants.Mediator))
                    throw new NullReferenceException("No Mediator was registered");
                return Application.Current.Resources[Constants.Mediator] as IMediator;
            }
        }
        public static SynchronizationContext SyncContext
        {
            get
            {
                if (!Application.Current.Resources.Contains(Constants.SynchronizationContext))
                    throw new NullReferenceException("No SynchronizationContext was registered");
                return Application.Current.Resources[Constants.SynchronizationContext] as SynchronizationContext;
            }
        }

        //Helper functions        
        public static T Create<T>()
        {
            return CurrentApplicationDIContainer.Create<T>();
        }
        public static T GetCreate<T>()
        {
            return CurrentApplicationDIContainer.GetCreate<T>();
        }
        public static void CreateStore<T>()
        {
            CurrentApplicationDIContainer.CreateStore<T>();
        }
        public static T Get<T>()
        {
            return CurrentApplicationDIContainer.Get<T>();
        }
        public static T GetNamedConfigurationItem<T>(string name)
        {
            return CurrentApplicationDIContainer.GetNamedConfigurationItem<T>(name);
        }
        public static void Register<T>(DIContainer.Creator creator)
        {
            CurrentApplicationDIContainer.Register<T>(creator);
        }
        public static void RemoveInstance<T>()
        {
            CurrentApplicationDIContainer.RemoveInstance<T>();
        }
    }
#endif
}
